package aod.android.com.bernay.mine.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.mine.adapter.DynamicAdapter;
import aod.android.com.bernay.mine.module.DynamicBean;
import aod.android.com.bernay.mine.module.MyDynamicBean;
import aod.android.com.bernay.util.SharePreUtils;
import aod.android.com.bernay.view.activity.PhotoInfoActivity;
import aod.android.com.bernay.view.activity.VideoInfoActivity;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.bind.HiView;

/**
 * @author crated by zhixianuanyang on 2019/4/3 12:21
 * @Email jamesKing0812@gmail.com
 */
public class DynamicActivity extends BernayBaseActivity {

    @HiView(R.id.dynamicRecyclerView)
    RecyclerView mDynamicRecyclerView;

    private DynamicAdapter vedioAdapter;
    private List<DynamicBean> mBeans = new ArrayList<>();
    private final static int pageIndex = 1;
    private final static int pageSize = 12;

    private MyDynamicBean dynamicBean;

    @Override
    protected int initLayout() {
        return R.layout.activity_dynamic;
    }

    @Override
    protected void initData() {
        initTitle(getString(R.string.my_dynamic));
        getMyDynamic();
        mDynamicRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //添加自定义分割线
        DividerItemDecoration divider = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this,R.drawable.divider_mileage));
        mDynamicRecyclerView.addItemDecoration(divider);
        //添加数据源
        mBeans.addAll(DynamicBean.mDynamicBeans);
        //设置适配器
        vedioAdapter = new DynamicAdapter(this, mBeans);
        mDynamicRecyclerView.setAdapter(vedioAdapter);

        vedioAdapter.setOnItemClickListener(new DynamicAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Bundle bundle = new Bundle();
                //判断图片和
                if (mBeans.get(position).isAlbum()){
                    goActivity(PhotoInfoActivity.class,bundle);
                }else{
                    goActivity(VideoInfoActivity.class,bundle);
                }
            }
        });
    }

    /**
     * 获取我的动态列表数据
     */
    private void getMyDynamic() {
        String userId = SharePreUtils.newInstance(DynamicActivity.this).getUserId();
        String token =  SharePreUtils.newInstance(DynamicActivity.this).getToken();
        BernayApi.http().getMyDynamic(userId, token, pageIndex, pageSize, new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                Gson gson = new Gson();
                dynamicBean = gson.fromJson(json, MyDynamicBean.class);
                if (1 == dynamicBean.getStatus()){
                    //TODO
                }
            }

            @Override
            public void onHttpFailure(IOException e) {
                LogUtils.json(new Gson().toJson(e));
            }
        });
    }
}
